package bancosys.tec.rtm.impl.diagnostician;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.naming.Context;
import javax.naming.NamingException;

import org.apache.commons.beanutils.BeanMap;

import bancosys.tec.rtm.diagnostician.AbstractDiagnostician;
import bancosys.tec.rtm.diagnostician.DiagnosisBean;

/**
 * Diagnosticador genérico para JNDI
 */
public abstract class AbstractJNDIDiagnostician extends AbstractDiagnostician {

    /**
     * @param ctx contexto
     * @param address lookup address
     * @param bean bean
     */
    protected void lookup(Context ctx, String address, DiagnosisBean bean) {
        try {
            Object obj = ctx.lookup(address);
            Set entries = new BeanMap(obj).entrySet();
            for (Object o : entries) {
                Map.Entry entry = (Entry) o;
                bean.addDescription("        " + entry.getKey() + " = " + entry.getValue());
            }
        } catch (NamingException e) {
            bean.addDescription("        Error while looking up " + address + ": " + e.getMessage());
        }
    }

}